import {create} from 'zustand';
import { persist } from "zustand/middleware";
import { immer } from 'zustand/middleware/immer';

const useCat = create(
    persist(
        immer((set , get) => ({
            name: '家猫',
            color:'黄色',
            age:1,
            setName: () => {
                set(state => ({...state, name: '花猫'})); //没有采用immer中间件，需要手动解构
                set(state => {state.color = '花猫'}); // immer中间件可以直接修改state对象
            },

            addAge: async() => {
                const res = await Promise.resolve(parseInt(Math.random() * 10));
                console.log(res);
                set(state => {state.age ++})
            },
            restState:() => {
                set({name: '家猫',color:'黄色',age:1});
                useCat.persist.clearStorage();
            }
    })),{name: 'catStore',version:'1.0.0'}));



export default useCat;